/**
 * 
 */
package com.vector.ding.dao.mybatis;

import java.util.HashMap;
import java.util.Map;

import com.vector.ding.commons.util.spring.SpringUtil;

/**
 * @author vector
 *
 */
public final class MapperFactory {
	
	private static Map<String, Object> mapperMap = new HashMap<String, Object>();
	
	private static MapperFactory factory = new MapperFactory();
	
	// no way to instance
	private MapperFactory() {
		
	}
	
	/**
	 * get mapper factory instance
	 * @return
	 */
	public static MapperFactory getInstance() {
		return factory;
	}
	
	/**
	 * @param mapperClass
	 * @return	mapper class object 
	 */
	@SuppressWarnings("unchecked")
	public <T> T getMapper(Class<T> mapperClass) {
		T mapper = (T) mapperMap.get(mapperClass.getSimpleName());
		if (null == mapper) {
			mapper = SpringUtil.getBean(mapperClass);
			mapperMap.put(mapperClass.getSimpleName(), mapper);
		}
		
		return mapper;
	}
}
